<?php
namespace Apps\BoxEdu\Model;

use Think\Model;

class EduExamsModel extends \BaseModel
{

    /*****试卷***/
    public function load($id = "")
    {
        $data = $this->find($id);
        if (!$data) {
            return false;
        }
        $id = $data['examsubject'];
        $data['examsetting'] = json_decode($data['examsetting'], true);
        $model = BoxModel("edu_questype");
        $EduQuestions = BoxModel('EduQuestions');
        $count = 0;
        foreach ($data['examsetting']['questype'] as $k => $one) {
            $questype = $model->find($k);
            $data['examsetting']['questype'][$k]['questype'] = $questype;
            $questions = $data['examsetting']['questype'][$k]['examquestions'] = $EduQuestions->ByPks($one['questions']);
            if (is_array($questions)) {
                $count += count($questions);
            } else {
                unset($data['examsetting']['questype'][$k]);
            }
        }
        $data['count'] = $count; //题量
        $data['examscore'] = json_decode($data['examscore'], true);
        $data['subject'] = BoxModel("EduSubject")->load($data['subjectid']);
        return $data;
    }

    /*******考试记录***/
    public function load_history($ehid)
    {
        $history_data = BoxModel("edu_examhistory")->load($ehid);
        $data = json_decode($history_data['exams_data'], true);
        $marking = json_decode($history_data['marking'], true);
        foreach ($data['examsetting']['questype'] as $k => $one) {
            foreach ($one['examquestions'] as $k2 => $one2) {
                $type = array();
                if ($marking['wrong_question_ids'][$one2['questionid']]) { //
                    $type[] = "wrong_questions"; //已经做错题
                    $type[] = "wrong_all_questions";//已经做错题+未做错题
                    $type[] = "done_questions"; //已做题数
                } elseif ($marking['right_question_ids'][$one2['questionid']]) {
                    $type[] = "right_questions";//对题数
                    $type[] = "done_questions";//已做题数
                } elseif ($marking['not_done_question_ids'][$one2['questionid']]) {
                    $type[] = "not_done_questions";//未做题数
                    $type[] = "wrong_all_questions";//已经做错题+未做错题
                }
                $one['examquestions'][$k2]['marking'] = implode(" ", $type);
            }
            $data['examsetting']['questype'][$k] = $one;
        }
        $data['marking'] = $marking;;
        return $data;
    }

    /**********试卷列表*****/
    public function get($where, $limit = "", $orderBy = "examid desc")
    {
        $count = $this->where($where)->count();
        if ($limit) {
            $page = setPager($count, $limit);
            $limit = $page['firstRow'] . "," . $page['listRows'];
        }
        $data = $this->where($where)->field("examid")->limit($limit)->order($orderBy)->select();
        if ($data) {
            foreach ($data as $one) {
                $re[] = $this->load($one['examid']);
            }
        }
        if ($data && $limit) {
            $data = array(
                'count' => $count,
                'list' => $re,
                'pager' => $page,
            );
        } else {
            $data = $re;
        }
        return $data;
    }
    /********考试记录***********/

}
 